/**
 * Created by Administrator on 2015/12/22.
 */
var Promise = require('promise');
var logger = require('log4js').getLogger('articleService');
var comm = require('../../utils/commonUtils');
var mailUtil = require('../../utils/mail');
var articleDao = require('../dao/ArticleDao');
var attentionDao = require('../dao/AttentionDao');
var likeDao = require('../dao/LikeDao');
var collectDao = require('../dao/CollectDao');
var memberDao = require('../dao/MemberDao');
var commentDao = require('../dao/CommentDao');
var tipoffRecordDao = require('../dao/TipoffRecordDao');
var config = require('../../../config')
var util = require('util');
var fs = require('fs');
var commonUtils = require('./../../utils/commonUtils');

var articleService = {};

articleService.pageForTime = function(pageNo ,pageSize){
    return new Promise(function(fulfil,reject){
        var params = {};
        params.bak1 = 0;
        articleDao.getByPage(params,pageNo,pageSize,"create_time desc").then(function(articles){
            fulfil(articles);
        },function(err){
            reject(err)
        })
    })
}

articleService.pageForLike = function(pageNo,pageSize){
    return new Promise(function(fulfil,reject){
        var params = {};
        params.bak1 = 0;
        articleDao.getByPage(params,pageNo,pageSize,"bak3 desc ,like_num desc").then(function(articles){
            fulfil(articles);
        },function(err){
            reject(err)
        })
    })
}
articleService.getById = function(id){
    return new Promise(function(fulfil,reject){
        articleDao.getById(id).then(function(article){
            fulfil(article);
        },function(err){
            reject(err);
        })
    })
}
articleService.save = function(article){
    return articleDao.save(article);
}

articleService.attention = function(attention){
    return attentionDao.save(attention);
}

articleService.like = function(like){
    return likeDao.getByParams(like,'id').then(function (l) {
        if(l&& l.length>0){
            return Promise.resolve(99);
        }else{
            like.createTime = new Date();
            return likeDao.save(like);
        }
    })

}
articleService.unLike = function(like){
    return likeDao.getByParams(like,'id').then(function (l) {
        if(l&& l.length>0){
            var id = l[0].id;
            return likeDao.delete(id);
        }else{
            return Promise.resolve(99);
        }
    })

}

articleService.collect = function(collect){
    return collectDao.getByParams(collect,'id').then(function (c) {
        if(c&& c.length>0){
            return Promise.resolve(99);
        }else{
            collect.createTime = new Date();
            return collectDao.save(collect);
        }
    })

}

articleService.unCollect = function(collect){
    return collectDao.getByParams(collect,'id').then(function (c) {
        if(c&& c.length>0){
            var id = c[0].id;
            return collectDao.delete(id);
        }else{
            return Promise.resolve(99);
        }
    })

}
articleService.comment = function(comment){
    return commentDao.save(comment);
}

articleService.checkCollect = function (memberId, artId) {
    var params = {};
    params.articleId = artId;
    params.memberId = memberId;
    params.status = 0;
    return collectDao.getByParams(params,'id');


}
articleService.checkLike = function (memberId,artId) {
    var params = {};
    params.articleId = artId;
    params.memberId = memberId;
    params.status = 0;
    return likeDao.getByParams(params,'id');

}

articleService.getFileName = function(item,i){

    return new Promise(function(fulfil,reject){
        var date = new Date();
        var imgDate = date.getFullYear()+"/"+(parseInt(date.getMonth())+1)+"/"+date.getDate()+"/";
        commonUtils.mkdirsSync(config.main.fileUploadPath+imgDate);
        var suffix =/\.[^\.]+/.exec(item.path);
        var fileName = commonUtils.getFileName(config.main.fileUploadPath,suffix[0],i);
        var absFileName = config.main.fileUploadPath+imgDate+fileName;
        fs.rename(item.path,absFileName,function(err){
            if(err){
                reject(null);
            }else{
                fulfil(imgDate+fileName);
            }
        })
    })

}



articleService.plusComment = function(article){
    return articleDao.update(article);
}
articleService.plusCollect = function(article){
    return articleDao.update(article);
}
articleService.plusLike = function(article){
    return articleDao.update(article);
}

articleService.search = function(content,pageNo,pageSize){

    return articleDao.search(content,pageNo,pageSize);
}


articleService.find = function (type,orderBy,pageNo,pageSize) {
    var params = {};
    params.type = type;
    params.bak1 = 0;
    var order = '';
    if(1==orderBy){
        order = 'create_time desc';
    }else if(2==orderBy){
        order = 'like_num desc';
    }else{
        order = 'id';
    }
    return articleDao.getByPage(params,pageNo,pageSize,order);
}

articleService.delete = function (id) {


    return articleDao.delete(id).then(function () {
        var params = {};
        params.articleId = id;
        params.status = 0;
        return collectDao.deleteByParam(params).then(function () {
            return Promise.resolve(true);
        });
    });
}


articleService.update = function (ar) {
    return articleDao.update(ar);
}


articleService.getLikes = function (articleId,pageNo,pageSize) {
    var params = {};
    params.articleId = articleId;
    return memberDao.getLikeMemberByPage(params,pageNo,pageSize);
}


articleService.getUnLookCommentNum = function (articleId) {
    return articleDao.getUnLookCommentNum(articleId);
}



articleService.tipoffRecord = function (id, content) {
    var params = {};
    params.articleId = id;
    params.tipoff = content;
    params.status = 0;
    params.read = 0;
    params.createTime = new Date();
    return tipoffRecordDao.save(params);
}
module.exports = articleService;
